<?php
require_once(DIR . "/includes/adminfunctions.php");

define("VBMS_EVENT_DEBUG", -10);
define("VBMS_EVENT_INFO", 0);
define("VBMS_EVENT_WARNING", 10);
define("VBMS_EVENT_ERROR", 20);

$vbms_event_names = array
(
	VBMS_EVENT_DEBUG => $vbphrase['debug'],
	VBMS_EVENT_INFO => $vbphrase['information'],
	VBMS_EVENT_WARNING => $vbphrase['warning'],
	VBMS_EVENT_ERROR => $vbphrase['error']
);

class vbms_logger
{
	var $containserrors;
	var $key;
	var $events;
	var $sourcekey;

	function vbms_logger($realtimeecho = false)
	{
		$this->sourcekey = THIS_SCRIPT;
		$this->rte = $realtimeecho;
	}
	
	function log_event($text, $type = VBMS_EVENT_INFO, $trailingbr = true)
	{
		if ($type > VBMS_EVENT_INFO)
		{
			$this->containserrors = true;
		}
		
		$event = array
		(
			"dateline" => time(),
			"type" => $type,
			"text" => $text
		);
		$this->events[] = $event;
		
		if ($this->rte)
		{
			global $vbms_event_names;
			
			if ($type > VBMS_EVENT_INFO)
			{
				echo "<strong><em>" . $vbms_event_names[$type] . ":</em></strong> ";
			}
			else
			{
				echo "<em>" . $vbms_event_names[$type] . "</em>: ";
			}
			echo $text;
			if ($trailingbr)
			{
				echo "<br />";
			}
			vbflush();
		}
	}
	
	function save_to_database()
	{
		global $vbulletin;
		
		$vbulletin->db->query(
				"INSERT INTO " . TABLE_PREFIX . "vbms_log
				(sourcekey, dateline, containserrors, serializedform)
				VALUES
				(\"" . addslashes($this->sourcekey) . "\", " . TIMENOW . ", " .
				intval($this->containserrors) . ", \"" .
				addslashes(serialize($this)) . "\")");
	}
	
	function print_admincp_html_table($htmlify = false)
	{
		global $vbulletin, $vbms_event_names, $vbphrase;
		
		print_form_header();
		
		if (!empty($this->events))
		{
			$date = vbdate($vbulletin->options['dateformat'], $this->events[0]['dateline']);
			$time = vbdate($vbulletin->options['timeformat'], $this->events[0]['dateline']);
		
			print_table_header(construct_phrase($vbphrase['viewing_log_from_x_at_y'], $date, $time), 3);
		}
		else
		{
			print_table_header($vbphrase['view_log'], 3);
		}
		
		print_cells_row(array
		(
			$vbphrase['severity'],
			$vbphrase['date'] . "/" . $vbphrase['time'],
			"<span style=\"float: left\">" . $vbphrase['message'] . "</span>" // sigh
		), true);
		
		if (empty($this->events))
		{
			print_description_row($vbphrase['nothing_to_do'], false, 3);
		}
		else
		{
			foreach ($this->events as $event)
			{
				$date = vbdate($vbulletin->options['dateformat'], $event['dateline']);
				$time = vbdate($vbulletin->options['timeformat'], $event['dateline']);
				
				$type = $event['type'];
				if ($type > VBMS_EVENT_INFO)
				{
					$type = "<strong>" . $vbms_event_names[$type] . "</strong>";
				}
				else
				{
					$type = $vbms_event_names[$type];
				}
				
				$class = fetch_row_bgclass();
				
				echo "<tr>
						<td nowrap=\"nowrap\" class=\"$class\">$type</td>
						<td nowrap=\"nowrap\" class=\"$class\">$date $time</td>
						<td width=\"100%\" class=\"$class\">" .
						($htmlify ? htmlspecialchars_uni($event['text']) : $event['text']) . "</td>
					</tr>";
			}
		}
		
		print_table_footer(3);
	}
}
?>